package controllers;

import java.util.Collection;

import model.CarmenSandiegoAppModel;
import play.libs.Json;
import play.mvc.Controller;
import play.mvc.Result;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

import dominio.individuos.Villano;
import dominio.investigacion.OrdenDeArresto;
import dominio.ubicaciones.Pais;

public class Application extends Controller {
    
	
	public static Result paisActual() {
		response().setContentType("application/json");
		Pais paisActual = CarmenSandiegoAppModel.getInstance().getPaisActual();
		JsonNode json = Json.toJson(paisActual);
		return ok(json);
	}
	
	public static Result paisAnterior() {
		response().setContentType("application/json");
		Pais paisAnterior = CarmenSandiegoAppModel.getInstance().getPaisAnterior();
		JsonNode json = Json.toJson(paisAnterior);
		return ok(json);
	}
	
	public static Result pais(String nombrePais) {
		response().setContentType("application/json");
		Pais pais = CarmenSandiegoAppModel.getInstance().getMapamundi().buscarPais(nombrePais);
		JsonNode json = Json.toJson(pais);
		return ok(json);
	}
	
	public static Result villanos() {
    	response().setContentType("application/json");
    	Collection<Villano> villanos = CarmenSandiegoAppModel.getInstance().getExpedientes().getVillanos();
    	JsonNode json = Json.toJson(villanos);
    	return ok(json);
    }
	
	public static Result viajar(String pais) {
    	//Pais actualizado = Json.fromJson(request().body().asJson(), Pais.class);
		Pais destino = CarmenSandiegoAppModel.getInstance().getMapamundi().buscarPais(pais);
    	CarmenSandiegoAppModel.getInstance().viajar(destino);
    	return ok(crearJsonOk());
    }
	
	public static Result villanoResponsable() {
		response().setContentType("application/json");
    	Villano villano = CarmenSandiegoAppModel.getInstance().getVillanoResponsable();
    	JsonNode json = Json.toJson(villano);
    	return ok(json);
	}
	
	public static Result emitirOrden(String villano) {
		Villano vil = CarmenSandiegoAppModel.getInstance().getExpedientes().buscarVillano(villano);
		CarmenSandiegoAppModel.getInstance().setAcusado(vil);
		return ok(crearJsonOk());
	}
	
	public static Result acusado() {
		response().setContentType("application/json");
		Villano villano = CarmenSandiegoAppModel.getInstance().getAcusado();
		JsonNode json = Json.toJson(villano);
		return ok(json);		
	}
	
	public static Result rutaRecorrida() {
		response().setContentType("application/json");
		JsonNode json = Json.toJson(CarmenSandiegoAppModel.getInstance().getRutaRecorrida());
		return ok(json);
	}
	
	public static Result gano() {
		response().setContentType("application/json");
		JsonNode json = Json.toJson(CarmenSandiegoAppModel.getInstance().getGano());
		return ok(json);
	}
	
	///////////////////////////////////
	///////////////////////////////////
	
	protected static ObjectNode crearJsonError(String message) {
    	ObjectNode result = Json.newObject();
		result.put("error", message);
		return result;
	}
    
    protected static ObjectNode crearJsonOk() {
    	ObjectNode result = Json.newObject();
		result.put("status", "OK");
		return result;
	}
	
    
   
}
